// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   FtpServerListener.java

package org.baixin.ftp.listener;

import javax.servlet.*;
import org.apache.ftpserver.FtpServer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class FtpServerListener
	implements ServletContextListener
{

	public static final String FTPSERVER_CONTEXT_NAME = "org.apache.ftpserver";

	public FtpServerListener()
	{
	}

	public void contextDestroyed(ServletContextEvent sce)
	{
		System.out.println("Stopping FtpServer");
		FtpServer server = (FtpServer)sce.getServletContext().getAttribute("org.apache.ftpserver");
		if (server != null)
		{
			server.stop();
			sce.getServletContext().removeAttribute("org.apache.ftpserver");
			System.out.println("FtpServer stopped");
		} else
		{
			System.out.println("No running FtpServer found");
		}
	}

	public void contextInitialized(ServletContextEvent sce)
	{
		System.out.println("正在初始化 FtpServer");
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		FtpServer server = (FtpServer)ctx.getBean("myServer");
		sce.getServletContext().setAttribute("org.apache.ftpserver", server);
		try
		{
			server.start();
			System.out.println("FtpServer started");
		}
		catch (Exception e)
		{
			throw new RuntimeException("Failed to start FtpServer", e);
		}
	}
}
